<?php

namespace App\Admin\Controllers\Bureau;

use App\Admin\Actions\Grid\Bureau\RefuseBureauNoticeAction;
use App\Admin\Repositories\Bureau\WxBureau;
use App\Wen\Utils\ImageUtils;
use App\Wen\Utils\Settings;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxBureauController extends AdminController
{
    protected $status_arr = [
        0 => '审核中',
        1 => '正常',
        2 => '已下架',
        3 => '管理员下架',
        4 => '被举报下架'
    ];
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxBureau(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->model()->orderBy('id','desc');
            $grid->quickSearch(['id', 'title','intro', 'content', 'user_id', 'ower.user_name', 'address_detailed'])->placeholder('搜索ID，标题，简介，用户ID，用户名字，地址...')->width(50);
            $grid->column('owers', '头像')->display(function ($v) {
                return '<img src="' . ($this->ower['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('ower', '用户名称')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');
            $grid->column('club_id');
            $grid->column('title')->limit(30);
            $grid->column('intro')->limit(50);

//            $grid->column('content');
//            $grid->column('image_urls');
//            $grid->column('video_url');
//            $grid->column('video_cover');
//            $grid->column('tags');
//            $grid->column('service_date_start');
//            $grid->column('service_date_end');
//            $grid->column('service_time_start');
//            $grid->column('service_time_end');
//            $grid->column('longitude');
//            $grid->column('latitude');
//            $grid->column('address_name');
//            $grid->column('address_detailed');
//            $grid->column('ip');
//            $grid->column('country');
//            $grid->column('province');
//            $grid->column('city');
//            $grid->column('district');
//            $grid->column('refund_policy');
//            $grid->column('exposure_type');
//            $grid->column('user_info_fill');
            $grid->column('order')->editable();
            $grid->column('status')->select($this->status_arr);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append((new RefuseBureauNoticeAction())->setKey($this->id));
            });

            $grid->disableCreateButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxBureau(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('club_id');
            $show->field('title');
            $show->field('intro');
            $show->field('content');
            $show->field('image_urls');
            $show->field('video_url');
            $show->field('video_cover');
            $show->field('tags');
            $show->field('service_date_start');
            $show->field('service_date_end');
            $show->field('service_time_start');
            $show->field('service_time_end');
            $show->field('longitude');
            $show->field('latitude');
            $show->field('address_name');
            $show->field('address_detailed');
            $show->field('ip');
            $show->field('country');
            $show->field('province');
            $show->field('city');
            $show->field('district');
            $show->field('refund_policy');
            $show->field('exposure_type');
            $show->field('user_info_fill');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $app_bureau_tags = Settings::get('app_bureau_tags', []);
        $app_bureau_tags_new = [];
        if($app_bureau_tags){
            foreach ($app_bureau_tags as $tag){
                $app_bureau_tags_new[$tag] = $tag;
            }
        }
        return Form::make(new WxBureau(), function (Form $form) use (&$app_bureau_tags_new){
            $model = $form->model();

            $form->display('id');
//            $form->text('user_id');
//            $form->text('club_id');
            $form->text('title');
            $form->textarea('intro');
            if(__system_is_model_enable('laradocs', 'dcat-neditor')){
                $form->neditor('content');
            }else{
                $form->editor('content');
            }
            $form->multipleImage('image_urls')->limit(100)->url('files/uploads')->uniqueName()->autoUpload();

            $form->file('video_url')->maxSize(10240)->accept('mp4');
            $form->image('video_cover')->url('files/uploads')->uniqueName()->autoUpload();
            $form->select('tags')->options($app_bureau_tags_new);
            $form->dateRange('service_date_start', 'service_date_end', '服务日期');
            $form->timeRange('service_time_start', 'service_time_end', '服务时间');
//            $form->text('longitude');
//            $form->text('latitude');
//            $form->text('address_name');
            $form->text('address_detailed');
//            $form->text('ip');
//            $form->text('country');
//            $form->text('province');
//            $form->text('city');
//            $form->text('district');
            $form->select('refund_policy')->options([
                0 => '活动开始前随时可以退款',
                3 => '活动开始前3个小时可以退款',
                12 => '活动开始前12个小时可以退款',
                24 => '活动开始前24个小时可以退款',
                48 => '活动开始前48个小时可以退款',
                72 => '活动开始前3天可以退款',
                168 => '活动开始前7天可以退款',
                240 => '活动开始前10天可以退款',
                255 => '活动不支持无理由退款',
            ]);
            $form->select('exposure_type')->options([
                1 => '公开（所有地方可见）',
                2 => '私密（不出现在首页）',
                3 => '私密（仅分享链接可见）'
            ]);
//            $form->textarea('user_info_fill');
            $form->radio('status')->options($this->status_arr);

//            $form->display('created_at');
//            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
